#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import re
import ast
import json
from collections import defaultdict
import argparse

def extract_data_points(text):
    """
    从文本中提取所有字典格式的数据点
    """
    # 匹配字典格式的模式
    pattern = r"\{[^{}]*(?:'[^']+'[^{}]*:[^{}]*\d+\.?\d*[^{}]*)*[^{}]*\}"
    matches = re.findall(pattern, text, re.DOTALL)
    
    data_points = []
    for match in matches:
        try:
            # 尝试使用ast安全解析
            data = ast.literal_eval(match)
            # 检查是否包含必要的字段
            if isinstance(data, dict) and 'TimeSatamp' in data:
                data_points.append(data)
        except:
            try:
                # 尝试使用json解析（需要将单引号转为双引号）
                json_str = match.replace("'", "\"")
                data = json.loads(json_str)
                if isinstance(data, dict) and 'TimeSatamp' in data:
                    data_points.append(data)
            except:
                continue
    
    return data_points

def calculate_statistics(data_points):
    """
    计算每个字段的统计信息
    """
    if not data_points:
        return None
    
    # 收集所有字段的值
    field_values = defaultdict(list)
    
    for point in data_points:
        for key, value in point.items():
            if isinstance(value, (int, float)):
                field_values[key].append(value)
    
    # 计算统计信息
    statistics = {}
    for field, values in field_values.items():
        if values:
            statistics[field] = {
                'count': len(values),
                'mean': sum(values) / len(values),
                'max': max(values),
                'min': min(values)
            }
    
    return statistics

def print_data_table(data_points):
    """
    按表格格式打印所有数据点
    """
    if not data_points:
        print("没有找到数据点")
        return
    
    # 获取所有字段名
    all_fields = set()
    for point in data_points:
        all_fields.update(point.keys())
    
    # 排序字段，将TimeSatamp放在第一位
    sorted_fields = sorted(all_fields)
    if 'TimeSatamp' in sorted_fields:
        sorted_fields.remove('TimeSatamp')
        sorted_fields = ['TimeSatamp'] + sorted_fields
    
    # 打印表头
    header = "| " + " | ".join(f"{field:<15}" for field in sorted_fields) + " |"
    separator = "|" + "|".join(["-" * 17 for _ in sorted_fields]) + "|"
    
    print("\n" + "=" * (len(header) + 2))
    print("内存统计数据表")
    print("=" * (len(header) + 2))
    print(header)
    print(separator)
    
    # 打印每一行数据
    for i, point in enumerate(data_points):
        row = "| "
        for field in sorted_fields:
            value = point.get(field, "N/A")
            if isinstance(value, float):
                row += f"{value:<15.2f} | "
            else:
                row += f"{str(value):<15} | "
        print(row)
    
    print("=" * (len(header) + 2))
    print(f"总计: {len(data_points)} 个数据点")

def print_statistics_table(statistics):
    """
    按表格格式打印统计信息
    """
    if not statistics:
        print("没有统计信息可显示")
        return
    
    # 准备表头和数据
    headers = ["字段名", "数据点数", "平均值", "最大值", "最小值"]
    rows = []
    
    for field, stats in statistics.items():
        rows.append([
            field,
            stats['count'],
            f"{stats['mean']:.2f}",
            f"{stats['max']:.2f}",
            f"{stats['min']:.2f}"
        ])
    
    # 计算列宽
    col_widths = [len(header) for header in headers]
    for row in rows:
        for i, cell in enumerate(row):
            col_widths[i] = max(col_widths[i], len(str(cell)))
    
    # 打印表头
    header_row = "| " + " | ".join(f"{header:<{col_widths[i]}}" for i, header in enumerate(headers)) + " |"
    separator = "+" + "+".join(["-" * (w + 2) for w in col_widths]) + "+"
    
    print("\n" + "=" * (len(header_row) + 2))
    print("统计信息汇总")
    print("=" * (len(header_row) + 2))
    print(separator)
    print(header_row)
    print(separator)
    
    # 打印数据行
    for row in rows:
        row_str = "| " + " | ".join(f"{str(cell):<{col_widths[i]}}" for i, cell in enumerate(row)) + " |"
        print(row_str)
    
    print(separator)

def create_sample_data():
    """
    创建示例数据用于测试
    """
    sample_data = """
    应用启动日志
    内存统计1: {'TimeSatamp': 1756796001.230954, 'GL': 65512, 'Graph': 127596, 'ark ts heap': 176468, 'native heap': 284574, 'AnonPage other': 365126, 'FilePage other': 57091, 'Total': 1203355, 'jemalloc heap': 274040}
    用户操作日志
    内存统计2: {"TimeSatamp": 1756796002.451234, "GL": 65890, "Graph": 128123, "ark ts heap": 177231, "native heap": 285672, "AnonPage other": 366543, "FilePage other": 57234, "Total": 1207890, "jemalloc heap": 275195}
    性能监控
    内存统计3: {'TimeSatamp': 1756796003.789012, 'GL': 66234, 'Graph': 128567, 'ark ts heap': 178456, 'native heap': 286789, 'AnonPage other': 367890, 'FilePage other': 57456, 'Total': 1212345, 'jemalloc heap': 276123}
    错误处理
    内存统计4: {"TimeSatamp": 1756796004.123456, "GL": 66543, "Graph": 129012, "ark ts heap": 179234, "native heap": 287456, "AnonPage other": 368765, "FilePage other": 57678, "Total": 1216789, "jemalloc heap": 277456}
    内存统计5: {'TimeSatamp': 1756796005.456789, 'GL': 66890, 'Graph': 129456, 'ark ts heap': 180123, 'native heap': 288123, 'AnonPage other': 369456, 'FilePage other': 57890, 'Total': 1221234, 'jemalloc heap': 278789}
    """
    
    with open('sample_data.txt', 'w') as f:
        f.write(sample_data)
    
    print("示例数据已创建: sample_data.txt")
    return sample_data

def main():
    parser = argparse.ArgumentParser(description='内存统计数据表格化工具')
    parser.add_argument('filename', nargs='?', help='要分析的文本文件路径')
    parser.add_argument('--sample', action='store_true', help='创建示例数据并分析')
    parser.add_argument('--stats-only', action='store_true', help='只显示统计信息，不显示原始数据')
    
    args = parser.parse_args()
    
    # 处理示例数据或文件
    if args.sample:
        text = create_sample_data()
        filename = "sample_data.txt"
    elif args.filename:
        try:
            with open(args.filename, 'r', encoding='utf-8') as f:
                text = f.read()
            filename = args.filename
        except UnicodeDecodeError:
            try:
                with open(args.filename, 'r', encoding='latin-1') as f:
                    text = f.read()
                filename = args.filename
            except Exception as e:
                print(f"读取文件失败: {e}")
                return
    else:
        print("请提供文件名或使用 --sample 参数")
        return
    
    # 提取数据点
    data_points = extract_data_points(text)
    
    if not data_points:
        print(f"在文件 {filename} 中未找到有效的数据点")
        return
    
    print(f"从 {filename} 中提取到 {len(data_points)} 个数据点")
    
    # 打印原始数据表格
    if not args.stats_only:
        print_data_table(data_points)
    
    # 计算并打印统计信息
    statistics = calculate_statistics(data_points)
    if statistics:
        print_statistics_table(statistics)
        
        # 额外信息
        print(f"\n字段说明:")
        print(f"- TimeSatamp: 时间戳")
        print(f"- GL: GPU内存使用")
        print(f"- Graph: 图形内存使用") 
        print(f"- ark ts heap: ArkTS堆内存")
        print(f"- native heap: 原生堆内存")
        print(f"- AnonPage other: 其他匿名页")
        print(f"- FilePage other: 其他文件页")
        print(f"- Total: 总内存使用")
        print(f"- jemalloc heap: jemalloc堆内存")

if __name__ == "__main__":
    main()